<?php
/**
 * ajax controller class
 *
 * @author raphael seebacher <raphasee@ee.ethz.ch>
 * @version 0.01
 */
class ajax_controller extends front_controller {
    protected $request = NULL;

    public static function is_valid_request_url($request) {
        return (substr($request->get_request_string(), 0, 4) == 'ajax');
    }

    public function process_request() {
        $request_string = rtrim(substr($this->request->get_request_string(), 5), '/');
        $user_controller = user_controller::get_instance();

        if (!$user_controller->is_authenticated()) {
            header('Status: 401 Unauthorized');
            return;
        }

        $user = $user_controller->get_user();

        switch ($request_string) {
        case 'location_reports/get':
            $location_reports = $user->get_location_reports();
            $result = Array();

            foreach ($location_reports as $lr) {
                $result[] = $lr->get_all();
            }

            header('Content-Type: application/json');
            echo json_encode($result);
            break;

        case 'connections/get':
            try {
                $connections = $user->get_connections();
                $result = Array();

                foreach ($connections as $connection) {
                    $result[] = Array(
                      'type' => ucfirst($connection->get('type')),
                      'mac' => $connection->get('mac'),
                      'lts' => $connection->get('lts'),
                    );
                }
            }
            catch (model_exception $e) {
                $result = Array();
            }

            header('Content-Type: application/json');
            echo json_encode($result);
            break;

        case 'certificate/get':
            try {
                $certificate = $user->get_current_certificate();
                $result = Array(
                  'valid_from' => $certificate->get('valid_from'),
                  'valid_to' => $certificate->get('valid_to'),
                  'certificate' => $certificate->get('certificate'),
                );
            }
            catch (model_exception $e) {
                $result = Array();
            }

            header('Content-Type: application/json');
            echo json_encode($result);
            break;

        case 'certificate/revoke':
            $result = Array(
              'status' => 'ok',
            );

            try {
                $certificate = $user->get_current_certificate();
                $certificate->revoke();
            }
            catch (model_exception $e) {
                $result['status'] = 'error';
            }

            header('Content-Type: application/json');
            echo json_encode($result);
            break;

        case 'ff/get':
            $ffs = $user->get_ff();
            $result = Array();

            foreach ($ffs as $ff) {
                $result[] = Array(
                  'name' => $ff->get_name(),
                  'screen_name' => $ff->get_screen_name(),
                );
            }
            
            header('Content-Type: application/json');
            echo json_encode($result);
            break;

        case 'ff/update':
            exec(
              '../scripts/background/synchronize_db_with_twitter.php '
              .$user->get_id()
            );
            break;

        default:
            header('Status: 404 Not Found');
        }
    }
}
?>